<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SkillAdminController
 *
 * @author dro
 */

namespace Gairal\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Gairal\CoreBundle\Form\SkillType;
use Gairal\CoreBundle\Entity\Skill;

class SkillAdminController extends Controller {

    public function indexAction() {
        //Call addAction to save a new Skill il request is POST
        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
            $this->addAction($request);

        $listSkills = $this->getDoctrine()
                ->getManager()
                ->getRepository('GairalCoreBundle:Skill')
                ->findAll();

        return $this->render('GairalAdminBundle:Skill:index.html.twig', array(
                    'action' => 'Admin - Skills',
                    'listSkills' => $listSkills
        ));
    }

    public function addAction($request = null) {

        $skill = new Skill();
        $form = $this->createForm(new SkillType(), $skill);

        if ($request == null) {
            return $this->render('GairalAdminBundle:Skill:add.html.twig', array(
                        'form' => $form->createView()
            ));
        }
        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
            if ($form->isValid()) {

                $em = $this->getDoctrine()->getManager();
                $em->persist($skill);
                $em->flush();

                $this->get('session')->getFlashBag()->add('info', 'Skill "' . $skill->getName() . '" added');
                return $this->redirect($this->generateUrl('AdminSkill'));
            }
            else
                $this->get('session')->getFlashBag()->add('error', $form->getErrorsAsString());
        }
    }

    public function removeAction(Skill $skill) {

        if ($skill != null) {
            $em = $this->getDoctrine()
                    ->getManager();
            $em->remove($skill);
            $em->flush();
            $this->get('session')->getFlashBag()->add('info', 'Skill "' . $skill->getName() . '" removed');
        }
        else
            $this->get('session')->getFlashBag()->add('error', 'Skill not removed, error in url');

        return $this->redirect($this->generateUrl('AdminSkill'));
    }

    public
            function modifyAction(Skill $skill) {
        $form = $this->createForm(new SkillType(), $skill);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
            if ($form->isValid()) {

                $em = $this->getDoctrine()->getManager();
                $em->persist($skill);
                $em->flush();

                $this->get('session')->getFlashBag()->add('info', 'Skill "' . $skill->getName() . '" modified');
                return $this->redirect($this->generateUrl('AdminSkill'));
            }
            else
                $this->get('session')->getFlashBag()->add('error', $form->getErrorsAsString());
        }
        return $this->render('GairalAdminBundle:Skill:modify.html.twig', array(
                    'action' => 'Admin - Skill - modify - ' . $skill->getName(),
                    'form' => $form->createView(),
                    'skill' => $skill
        ));
    }

}

?>
